<template>
	<view class="content">
		<image class="background" src="/static/background.png"></image>
		<button v-show="isShow" class="loginbtn" type="primary" lang="zh-CN" @tap="getUserProfile">授权登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return{
				isShow:false,
				baseURL:"<请自行填写>",
				serverURL:"<请自行填写>",
				clientID:"<请自行填写>"
			}
		},
		methods: {
			getUserProfile(){
				let _this = this;
				uni.getUserProfile({
					desc:'获取用户信息',
					success: userRes => {
						uni.showLoading({
							title: '登录中',
							mask: true
						});
						uni.login({
							success: loginRes => {
								if(loginRes.code){
									_this.verifyUser(loginRes.code, userRes);
								}
							},
							fail: (error) => {
								uni.hideLoading();
								uni.showModal({
									content: error,
									confirmText: '确定',
								});
							}
						})
					}
				})
			},
			verifyUser(code, userRes, openid){
				let _this = this;
				uni.request({
					url: _this.baseURL + "/ServerCommand/GetRequestToken",
					method:"POST",
					data:{
						"client_id": _this.clientID,
						"serverURL": _this.serverURL
					},
					success: requestTokenResult => {
						let url = null, data = null;
						if(userRes == null){
							url = _this.baseURL + "/ServerCommand/GetSSOToken";
							data = {
								"userName":openid,
								"baseURL":_this.baseURL
							}
						}else if(openid == null){
							url = _this.baseURL + "/ServerCommand/AddUserAndLogin";
							data = {
								"code":code,
								"fullname":userRes.userInfo.nickName,
								"avatarurl":userRes.userInfo.avatarUrl,
								"gender":userRes.userInfo.gender,
								"country":userRes.userInfo.country,
								"province":userRes.userInfo.province,
								"city":userRes.userInfo.city,
								"language":userRes.userInfo.language,
								"baseURL":_this.baseURL
							}
						}
						uni.request({
							url:url,
							method:"POST",
							header:{
								"Authorization":requestTokenResult.data.Authorization
							},
							data: data,
							success: result => {
								uni.setStorage({
									key:"openid",
									data:result.data.openid,
									success: () => {
										if(result.data.ErrCode ==0){
											uni.navigateTo({
												url:'/pages/index/webview?redirectURL=' + encodeURIComponent(result.data.redirectURL)
											})
										}
									}
								})
							}
						})
					}
				})
			}
		},
		onLoad() {
			let _this = this;
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			uni.login({
				success: loginRes => {
					if(loginRes.code){
						uni.request({
							url: _this.baseURL + "/ServerCommand/GetRequestToken",
							method:"POST",
							data:{
								"client_id":_this.clientID,
								"serverURL":_this.serverURL
							},
							success: requestTokenResult => {
								uni.request({
									url: _this.baseURL + "/ServerCommand/Code2SessionID",
									method:"POST",
									header:{
										"Authorization":requestTokenResult.data.Authorization
									},
									data:{
										"code":loginRes.code
									},
									success: code2SessionIDResult => {
										if(code2SessionIDResult.data.openid){
											uni.request({
												url: _this.baseURL + "/ServerCommand/GetOpenidCount",
												method:"POST",
												header:{
													"Authorization":requestTokenResult.data.Authorization
												},
												data:{
													"openid":code2SessionIDResult.data.openid
												},
												success: countResult => {
													if(countResult.data.Message > 0){
														uni.showLoading({
															title: '自动登录中',
															mask: true
														});
														_this.verifyUser(loginRes.code, null, code2SessionIDResult.data.openid);
													}else{
														uni.hideLoading();
														this.isShow = true;
													}
												}
											})
										}else{
											uni.showModal({
												content:code2SessionIDResult.data.Message,
												confirmText: '确定'
											})
										}
									}
								})
							}
						})
					}
				}
			})
		}
	}
</script>

<style>
.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.background{
		position: absolute;
		top:0;
		left:0;
		width:100%;
		height:100%;
		z-index:-1;
	}
	
	.loginbtn{
		position: absolute;
		bottom:200rpx;
	}
</style>
